#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.ComponentModel;
using System.Drawing.Design;
using XnaGameComponents;
#endregion

namespace WindowsGameLibrary1.Components
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Grid : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public Grid(Game game)
            : base(game)
        {
            width = 2;
            height = 2;
            xResolution = 16;
            yres = 16;
        }


        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
            linelist = new VertexPositionColor[(1 + xResolution * yres) * 4];
            for (int i = 0; i < linelist.Length; i += 4)
            {
                int pos = (i / 4);
                if (i + 4 != linelist.Length)
                {
                    linelist[i] = new VertexPositionColor(
                        new Vector3(-width / 2.0f + ((pos % xResolution) * width / (xResolution * 1.0f)),
                            0.0f,
                            -height / 2.0f + ((pos / yres) * height / (yres * 1.0f))),
                        Color.Black);
                    linelist[i + 1] = new VertexPositionColor(
                        new Vector3(-width / 2.0f + (((pos % xResolution) + 1) * width / (xResolution * 1.0f)),
                            0.0f,
                            -height / 2.0f + ((pos / yres) * height / (yres * 1.0f))),
                        Color.Black);
                    linelist[i + 2] = new VertexPositionColor(
                        new Vector3(-width / 2.0f + ((pos % xResolution) * width / (xResolution * 1.0f)),
                            0.0f,
                            -height / 2.0f + ((pos / yres) * height / (yres * 1.0f))),
                        Color.Black);
                    linelist[i + 3] = new VertexPositionColor(
                        new Vector3(-width / 2.0f + ((pos % xResolution) * width / (xResolution * 1.0f)),
                            0.0f,
                            -height / 2.0f + ((pos / yres + 1) * height / (yres * 1.0f))),
                        Color.Black);
                }
                else
                {
                    linelist[i] = new VertexPositionColor(
                        new Vector3(-width / 2.0f,
                            0.0f,
                            height / 2.0f),
                        Color.Black);
                    linelist[i + 1] = new VertexPositionColor(
                        new Vector3(width / 2.0f,
                            0.0f,
                            height / 2.0f),
                        Color.Black);
                    linelist[i + 2] = new VertexPositionColor(
                        new Vector3(width / 2.0f,
                            0.0f,
                            -height / 2.0f),
                        Color.Black);
                    linelist[i + 3] = new VertexPositionColor(
                        new Vector3(width / 2.0f,
                            0.0f,
                            height / 2.0f),
                        Color.Black);
                }
            }
        }

        private Color color = Color.Black;

        public Vector3 VColor
        {
            get { return color.ToVector3(); }
            set { color = new Color(value); }
        }

        private int width;

        [Category("Dimensions")]
        [DefaultValue(2)]
        public int Width
        {
            get { return width; }
            set { width = value; }
        }

        private int height;

        [DefaultValue(2)]
        [Category("Dimensions")]
        public int Height
        {
            get { return height; }
            set { height = value; }
        }

        private int xResolution;

        [DefaultValue(16)]
        [Category("Dimensions")]
        public int XResolution
        {
            get { return xResolution; }
            set { xResolution = value; }
        }

        private int yres;

        [DefaultValue(16)]
        [Category("Dimensions")]
        public int YResolution
        {
            get { return yres; }
            set { yres = value; }
        }

        VertexPositionColor[] linelist;

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Camera camera = (Camera)Game.Services.GetService(typeof(Camera));
            base.Draw(gameTime);
            if (camera != null)
            {
                BasicEffect be = new BasicEffect(GraphicsDevice);
                be.DiffuseColor = color.ToVector3();
                //be.World = camera.World;
                be.Projection = camera.Projection;
                be.View = camera.View;
                VertexBuffer vb = new VertexBuffer(GraphicsDevice, VertexPositionColor.VertexDeclaration, linelist.Length, BufferUsage.None);
                vb.SetData<VertexPositionColor>(linelist);
                //GraphicsDevice.Vertices[0].SetSource(vb, 0, VertexPositionColor.SizeInBytes);
                //be.Begin();
                foreach (EffectPass pass in be.CurrentTechnique.Passes)
                {
                    pass.Apply();
                    GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, linelist, 0, linelist.Length / 2);
                    //pass.End();
                }
                //be.End();
            }
        }


    }
}


